function newtrig = removeRepeatedTrigger(trig,nMinTrigSamp)
% newtrig = removeRepeatedTrigger(trig,nMinTrigSamp)
% removes repeated numbers greater than zero from a row vector trig
% nMinTrigSamp (optional) is the number of samples to ignore short trigger
% distances (eg. round(0.03*srate) ignores trigger following less than 30ms
% its preceding

if nargin<2,
    nMinTrigSamp = 1;
end

trigFlags = diff([0 trig]);
trigIdx=find(trig~=0 & trigFlags~=0 & trigFlags~=-trig);
trigIdxDist = diff([-nMinTrigSamp trigIdx]);

% remove unrealistic trigger distances
trigIdx(trigIdxDist<nMinTrigSamp)=[];

newtrig = zeros(size(trig));
newtrig(trigIdx)=trig(trigIdx);
